package cn.diancan.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Version;

import cn.diancan.util.DateUtil;
 

 
/**
 * 权限表
 * @author Administrator
 *
 */
@Entity
public class SysAuthority implements Serializable{
	/**
     * 
     */
    private static final long serialVersionUID = -8278396780871808443L;
	private Long    id;          //id
	private String url;     //url地址
	private String name;     //权限名称
	private Set<SysRole> ymRoles=new HashSet<SysRole>();//角色集合 
	private SysAuthority parent;//上一级 多对一
	private Set<SysAuthority> children=new HashSet<SysAuthority>();//下一级集合  一对多
	private String creationDate; //创建时间
	private String modificationDate;//修改时间
	private Integer enable;      //是否可用 1：可用 0：禁用
	private Integer isDelete;     //是否删除 0:未删除 1：删除
	private int indexNo;//排序 为保证排序有效，初始化时与id一致
	    private Integer version;
	    
	    @Version
	    @Column(name="OPTLOCK",columnDefinition="int(11) DEFAULT 0")
	    public Integer getVersion() {
	        return version;
	    }
	    public void setVersion(Integer version) {
	        this.version = version;
	    }
	public SysAuthority() {
		
	}
	public SysAuthority(String url, String name, SysAuthority parent) {	
		this.url = url;
		this.name = name;
		this.parent = parent;
		this.creationDate = DateUtil.currentDateTime();
		this.modificationDate= DateUtil.currentDateTime();
		this.enable=1;
		this.isDelete=0;
		this.indexNo=0;
	}
	
	@Id
	@GeneratedValue
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	@Column(name="creationDate",columnDefinition="DATETIME",length=19)
	public String getCreationDate() {
		return creationDate;
	}
	
	public void setCreationDate(String creationDate) {
		this.creationDate = creationDate;
	}
	
	@Column(name="modificationDate",columnDefinition="DATETIME",length=19)
	public String getModificationDate() {
		return modificationDate;
	}
	
	public void setModificationDate(String modificationDate) {
		this.modificationDate = modificationDate;
	}

	public Integer getEnable() {
		return enable;
	}

	public void setEnable(Integer enable) {
		this.enable = enable;
	}

	public Integer getIsDelete() {
	    return isDelete;
	}

	public void setIsDelete(Integer isDelete) {
	    this.isDelete = isDelete;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	
	@ManyToMany(
			cascade={CascadeType.PERSIST,CascadeType.MERGE },
			mappedBy="ymAuthorities",
			targetEntity=SysRole.class
			
			)
	public Set<SysRole> getYmRoles() {
		return ymRoles;
	}
	public void setYmRoles(Set<SysRole> ymRoles) {
		this.ymRoles = ymRoles;
	}
	
	@OneToMany(targetEntity=SysAuthority.class, mappedBy="parent", cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @OrderBy(value = "indexNo ASC")
	public Set<SysAuthority> getChildren() {
		return children;
	}
	public void setChildren(Set<SysAuthority> children) {
		this.children = children;
	}
	
	@ManyToOne(fetch = FetchType.LAZY,cascade=CascadeType.REMOVE) 
	@JoinColumn(name = "parent_id")
	@OrderBy(value = "indexNo ASC")
	public SysAuthority getParent() {
		return parent;
	}
	public void setParent(SysAuthority parent) {
		this.parent = parent;
	}
	public int getIndexNo() {
		return indexNo;
	}
	public void setIndexNo(int indexNo) {
		this.indexNo = indexNo;
	}

}
